package com.comp490.pattern;

import java.util.List;

import com.comp490.bullet.Bullet;
import com.comp490.shooter.GameView;
import com.comp490.sprite.Ship;

public class SpiralPattern extends BulletPattern
{
    private final int N;
    private final float STEP;
    private final float ROTATION;
    private float delta;
    private float[][] theta;

    /**
     * Constructor
     * 
     * @param x
     *            x pixel coordinate where the pattern should be centered
     * @param y
     *            y pixel coordinate where the pattern should be centered
     * @param step
     *            displacement in number of pixels
     * @param rotation
     *            angle of rotation in degrees (positive - clockwise, negative -
     *            counter-clockwise)
     * @param b
     *            list of Bullet objects to be arranged in the pattern
     */
    public SpiralPattern(float x, float y, float step, float rotation,
            List<Bullet> b, GameView gv, boolean act)
    {
        super(b, gv, act);
        STEP = step;
        ROTATION = toRadians(rotation);
        delta = step;
        bullets = b;
        N = bullets.size();
        theta = new float[3][N];

        for(int i = 0; i < N; i++)
        {
            theta[2][i] = 2 * i * PI / N;
            theta[0][i] = android.util.FloatMath.cos(theta[2][i]);
            theta[1][i] = android.util.FloatMath.sin(theta[2][i]);
        }
    }

    public void moveAndCheckCollision(List<? extends Ship> ships)
    {
        if(!active)
            return;
        active = false;
        
        moveLine(2f, 180);
        // update position of bullets and angles for next function call
        for(int i = 0; i < N; i++)
        {
            Bullet bullet = bullets.get(i);
            if(!bullet.isActive())
                continue;
            
            bullet.setX(delta * theta[0][i] + x);
            bullet.setY(delta * theta[1][i] + y);
            theta[2][i] += ROTATION;
            theta[0][i] = android.util.FloatMath.cos(theta[2][i]);
            theta[1][i] = android.util.FloatMath.sin(theta[2][i]);
            if(!bullet.boundsCheck(gameView))
            {
                bullet.setActive(false);
                continue;
            }
            
            for(int j = 0; j < ships.size(); j++)
            {
            	if(ships.get(j).isActive() && bullet.collisionCheck(ships.get(j)))
                {
            		ships.get(j).hit(bullet.getDamage());
                    bullet.setActive(false);
                    break;
                }
            }
            active |= bullet.isActive();
        }
        delta += STEP;
    }

    public String toString()
    {
        return bullets.toString();
    }

    @Override
    public void reset()
    {
    	for(int i = 0; i < bullets.size(); i++)
    	{
            bullets.get(i).setActive(true);
            bullets.get(i).setX(x);
            bullets.get(i).setY(y);
    	}
        active = true;
        delta = STEP;
    }
}